package com.sy.msgview.component

import android.content.Context
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.sy.msgview.QQItem
import com.sy.msgview.QQViewModel
import com.sy.msgview.Screen
import com.sy.msgview.Utils
import com.sy.msgview.ui.theme.Pink80

@Composable
fun QQItemView(qqItem: QQItem, clickEvent: (qq: Long) -> Unit) {
	Surface(modifier = Modifier
		.height(50.dp)
		.clickable {
			clickEvent(qqItem.qq)
		}) {
		Row(
			modifier = Modifier
				.fillMaxWidth()
				.padding(10.dp, 0.dp),
			verticalAlignment = Alignment.CenterVertically
		) {
			AsyncImage(
				model = "https://q1.qlogo.cn/g?b=qq&nk=${qqItem.qq}}&s=100",
				contentDescription = "avatar",
				modifier = Modifier.clip(RoundedCornerShape(5.dp))
			)
			Spacer(modifier = Modifier.width(5.dp))
			Text(
				text = qqItem.qq.toString(),
				color = if (qqItem.qq == 2509337306) Color.Red else Color.Unspecified
			)
		}
	}
}

@Composable
fun QQListView(model: QQViewModel, navController: NavController) {
	val qqList = model.getQQList().collectAsState(initial = listOf())
	Column {
		LazyColumn(contentPadding = PaddingValues(bottom = 15.dp)) {
			items(qqList.value) { item ->
				QQItemView(item) { qq ->
					navController.navigate(Screen.MSG.route + "/${qq}")
				}
			}
			item {
				Row(Modifier.fillMaxWidth(), Arrangement.Center, Alignment.CenterVertically) {
					Text(text = "到底啦", color = Color.Gray)
				}
			}
		}
	}
}

@Composable
fun Hitokoto() {
	var content by remember {
		mutableStateOf("我喜欢的样子，你都有。")
	}
	LaunchedEffect(null) {
		Utils.getRequest("https://v1.hitokoto.cn/?encode=text") {
			if (it.code == 200) content = it.body.string()
		}
	}
	Surface(
		modifier = Modifier
			.padding(10.dp)
			.heightIn(50.dp)
			.pointerInput(true) {
				detectTapGestures(onTap = {
					Utils.getRequest("https://v1.hitokoto.cn/?encode=text") {
						if (it.code == 200) content = it.body.string()
					}
				}, onDoubleTap = {  })
			},
		color = Pink80,
		shape = RoundedCornerShape(10.dp),
		contentColor = MaterialTheme.colorScheme.onPrimary
	) {
		Row(
			modifier = Modifier
				.padding(10.dp, 0.dp)
				.fillMaxWidth(),
			horizontalArrangement = Arrangement.Center,
			verticalAlignment = Alignment.CenterVertically
		) {
			Text(text = content, textAlign = TextAlign.Center)
		}
	}
}
